package com.ylcy.ads.event;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ylcy.ads.R;
import com.ylcy.ads.config.Code;
import com.ylcy.ads.config.ENV;
import com.ylcy.ads.launch.LaunchApp;
import com.ylcy.ads.schedule.LoopService;
import com.ylcy.ads.schedule.ScheduleService;
import com.ylcy.ads.service.RunningService;

public class BtnEvent {
    public static View.OnClickListener btnStartClick() {
        return new View.OnClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onClick(View view) {
                View root = view.getRootView();
                final TextView work_desc = root.findViewById(R.id.work_desc);
                work_desc.setText("正在启动...");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //用户编码
                final TextView eqId = root.findViewById(R.id.eqId);
                //规则刷新间隔
                final TextView returnTime = root.findViewById(R.id.return_time);
                final RadioGroup radioGroup = root.findViewById(R.id.radio_group);
                final int choseApp = radioGroup.getCheckedRadioButtonId();


                String pkgName = "钉钉".equals(((RadioButton) root.findViewById(choseApp)).getText()) ? Code.PKG_DINGDING : Code.PKG_WXWORK;
                ENV.context = view.getContext();
                ENV.pkgName = pkgName;
                ENV.eqId = eqId.getText().toString();
                ENV.retrunTime = Integer.parseInt(returnTime.getText().toString());

                //保存数据
                SharedPreferences.Editor editor = ENV.sharedPref.edit();
                editor.putString("eqId", ENV.eqId);
                editor.putInt("returnTime", ENV.retrunTime);
                editor.putInt("radioGroup", choseApp);
                editor.commit();
                //若选择的应用存在，则进入调度
                if (LaunchApp.check(view.getContext(), pkgName)) {
                    ENV.intent = new Intent(ENV.context, RunningService.class);
                    ENV.context.startService(ENV.intent);
                    work_desc.setText("调度服务正在执行...可在小程序端查看下次打卡时间");
                }
            }
        };
    }

    public static View.OnClickListener btnEndClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View root = view.getRootView();
                final TextView work_desc = root.findViewById(R.id.work_desc);
                work_desc.setText("正在停止调度服务...");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ENV.STIME_ = "";
                ENV.ETIME_ = "";
                //停止当前调度
                ENV.nm.cancel(9);
                if(ENV.context != null){
                    if(ENV.intent != null) {
                        ENV.context.stopService(ENV.intent);
                    }
                    ENV.context.stopService(new Intent(ENV.context, RunningService.class));
                }
                LoopService.stop();
                ScheduleService.stop();
                Toast.makeText(root.getContext(), "调度服务已终止!", Toast.LENGTH_LONG).show();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                work_desc.setText("软件调度服务已就绪...");
            }
        };
    }

}
